﻿#region Copyright (c) 2012-10, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Runtime.InteropServices;
using System.Text;


namespace Amarok.Native
{
	/// <summary>
	/// This class contains platform invokes to environment-related functions.
	/// </summary>
	internal static class UnsafeNativeEnvironment
	{
		// constants
		private const String LIBRARY = "kernel32.dll";


		/// <summary>
		/// Retrieves the fully-qualified path for the file that contains the specified module. The module must 
		/// have been loaded by the current process.
		/// </summary>
		/// 
		/// <param name="hModule">
		/// Handle to the loaded module whose path is being requested. If this parameter is NULL, GetModuleFileName 
		/// retrieves the path of the executable file of the current process.</param>
		/// <param name="fileName">
		/// Pointer to a buffer that receives the fully-qualified path of the module. If the length of the path 
		/// exceeds the size that the nSize parameter specifies, the function succeeds, and the string is truncated 
		/// to nSize characters and cannot be null terminated. The string returned will use the same format that was 
		/// specified when the module was loaded. Therefore, the path can be a long or short file name, and can use 
		/// the prefix "\\?\".</param>
		/// <param name="size">
		/// The size of the lpFilename buffer, in TCHARs.</param>
		/// 
		/// <returns>
		/// If the function succeeds, the return value is the length of the string that is copied to the buffer, 
		/// in TCHARs. If the buffer is too small to hold the module name, the string is truncated to nSize, the 
		/// function returns nSize, and the function sets the last error to ERROR_INSUFFICIENT_BUFFER.</returns>
		[DllImport(LIBRARY, SetLastError = true, CharSet = CharSet.Unicode)]
		public static extern UInt32 GetModuleFileName(IntPtr hModule, StringBuilder fileName, Int32 size);

	}
}
